<?php
/*
 * 3.php 页面的作用为，以后手机端可以访问这个界面,将配置写入单片机的配置数据库
 * */

include 'database.php';
require 'lianjie.php';
$f = 0;                     //用来表明配置信息是否受到修改
$id = $_GET['id'];
$atname = $_GET['atname'];
$mac = $_GET['mac'];           //mac地址
$kw = $_GET['kw'];            //智能模式下 温度临界值
$ks = $_GET['ks'];            //智能模式下 湿度临界值
$mode = $_GET['mode'];        //模式选择
$jt = $_GET['jt'];              //普通模式下定时浇水的时间间隔
$pt = $_GET['pt'];            //普通模式下定时喷雾的时间间隔
$at = $_GET['at'];            //设置各盆花的土壤湿度临界值
// $kw =  hexdec($kw);
//$ks = hexdec($ks);
$con = lianjieshujuku($servername, $name, $passwd);
if (!mysqli_select_db($con, 'flower')) {
    die("选择数据库失败");
}
$req = mysqli_query($con, "select * from `fname` WHERE `mac`='$mac'");
$m = mysqli_fetch_array($req);

//echo $m['mode'];
if ($m['mode'] != $mode) {
    mysqli_query($con, "update `fname` set `mode` ='$mode' where `mac`='$mac'");
    $f = 1;
}
if ($m['kongshi'] != $ks) {
    mysqli_query($con, "update `fname` set `kongshi` ='$ks' where `mac`='$mac'");
    $f = 1;
}
if ($m['kongwen'] != $kw) {
    mysqli_query($con, "update `fname` set `kongwen` ='$kw' where `mac`='$mac'");
    $f = 1;
}
if ($m['jt'] != $jt) {
    mysqli_query($con, "update `fname` set `jt` ='$jt' where `mac`='$mac'");
    $f = 1;
}
if ($m['pt'] != $pt) {
    mysqli_query($con, "update `fname` set `pt` ='$pt' where `mac`='$mac'");
    $f = 1;
}
/*if ($m['at'] != $mode) {
    mysqli_query($con, "update `fname` set `at` ='$at' where `mac`='$mac'");
    $f = 1;
}*/
if ($f == 1) {
    mysqli_query($con, "update `fname` set `op` ='0' where `mac`='$mac'");
}

if (!mysqli_select_db($con, 'flower')) {
    die("选择数据库失败");
}
$req = mysqli_query($con, "SELECT * from `fname` WHERE `mac`='$mac'");
$arry = mysqli_fetch_array($req);
$arryat = explode('|', $arry['at']);
$arryatname = explode('|', $arry['atname']);
//print_r($arryat);
//print_r($arryatname);
$arryat[$id - 1] = $at;

$arryatname[$id - 1] = $atname;
$newatnameall = "";
foreach ($arryatname as $newatname) {
    if ($newatnameall == "") {
        $newatnameall = $newatname;
    } else {
        $newatnameall = $newatnameall . "|" . $newatname;
    }
}
$newatall = "";
foreach ($arryat as $newat) {
    if ($newatall == "") {
        $newatall = $newat;
    } else {
        $newatall = $newatall . "|" . $newat;
    }
}
$req = mysqli_query($con, "UPDATE `fname` SET `at` = '$newatall',`atname`='$newatnameall' WHERE `mac` = '$mac'");
if ($req) {
    echo "成功fname";
}
    include 'database.php';
    require 'lianjie.php';
    $f = 0;                     //用来表明配置信息是否受到修改
    $mac = $_GET['mac'];           //mac地址
    $kw = $_GET['kw'];            //智能模式下 温度临界值
    $ks = $_GET['ks'];            //智能模式下 湿度临界值
    $mode = $_GET['mode'];        //模式选择
    $jt=$_GET['jt'];              //普通模式下定时浇水的时间间隔
    $pt = $_GET['pt'];            //普通模式下定时喷雾的时间间隔
    $at = $_GET['at'];            //设置各盆花的土壤湿度临界值
    // $kw =  hexdec($kw);
    //$ks = hexdec($ks);
    $con = lianjieshujuku($servername,$name,$passwd);
    if(!mysqli_select_db($con,'flower'))
    {
        die("选择数据库失败");
    }
    $req = mysqli_query($con,"select * from `fname`");
    $m = mysqli_fetch_array($req);
    if($m['mode']!=$mode)
    {
        mysqli_query($con,"update `fname` set `mode` ='$mode' where `mac`='$mac'");
        $f=1;
    }
    if($m['kongshi']!=$ks)
    {
        mysqli_query($con,"update `fname` set `kongshi` ='$ks' where `mac`='$mac'");
        $f=1;
    }
    if($m['kongwen']!=$kw)
    {
        mysqli_query($con,"update `fname` set `kongwen` ='$kw' where `mac`='$mac'");
        $f=1;
    }
    if($m['jt']!=$jt)
    {
        mysqli_query($con,"update `fname` set `jt` ='$jt' where `mac`='$mac'");
        $f=1;
    }
    if($m['pt']!=$pt)
    {
        mysqli_query($con,"update `fname` set `pt` ='$pt' where `mac`='$mac'");
        $f=1;
    }
    if($m['at']!=$mode)
    {
        mysqli_query($con,"update `fname` set `at` ='$at' where `mac`='$mac'");
        $f=1;
    }
    if($f==1)
    {
        mysqli_query($con,"update `fname` set `op` ='0' where `mac`='$mac'");
    }
